let storName
let storLogName
function listenerFunction(event) {
    // console.log('监听消息', event)
    if (event.data.type === 'dly_activity_modole') {
        console.log('监听到消息', event.data)
        storName = `dly_activity_list_${event.data.shop_id}`
        storLogName = `dly_auto_activity_logs_${event.data.shop_id}`

        switch (event.data.dly_method) {
            case 'add':
                // 情况所有chrome.storage.local
                // chrome.storage.local.clear(function () {
                //     console.log('清除成功');
                // })
                chrome.storage.local.get([storName], function (result) {
                    console.log('result.initTiyangfenData', result);
                    if (result[storName]) {
                        console.log('存在， 更新 或者新增')

                        chrome.storage.local.set({ [storName]: [...result[storName], event.data.data] }, function () {
                            console.log('新增加保存成功');
                            outActivityList()
                            sendAllPageMessage()
                            window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '保存成功' }, '*')
                        })
                    } else {
                        console.log('不存在，新增')

                        chrome.storage.local.set({ [storName]: [event.data.data] }, function () {
                            console.log('保存成功');
                            outActivityList()
                            sendAllPageMessage()
                            window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '保存成功' }, '*')
                        })
                    }
                });
                break;
            case 'updata':
                chrome.storage.local.get([storName], function (result) {
                    // console.log('result.initTiyangfenData', result.initTiyangfenData);
                    if (result[storName]) {
                        console.log('存在， 更新 或者新增')
                        if (!result[storName].find(item => item.id === event.data.data.id)) {
                            return console.log('不存在，无法完成更新')
                        }

                        chrome.storage.local.set({ [storName]: result[storName].map(item => item.id === event.data.data.id ? event.data.data : item) }, function () {
                            console.log('更新成功');
                            outActivityList()
                            sendAllPageMessage()
                            window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '更新成功' }, '*')
                        })
                    } else {
                        console.log('不存在，无法完成更新')
                    }
                });
                break;
            case 'get':
                // chrome.storage.local.set({ [storName]: [] }, function () {
                //     console.log('清空成功');
                //     outActivityList()
                //     window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '清空成功' }, '*')
                // })
                outActivityList()
                break;
            case 'get_logs':

                outAutoActivityLogs()
                break;
            case 'delete':
                chrome.storage.local.get([storName], function (result) {
                    // console.log('result.initTiyangfenData', result.initTiyangfenData);
                    if (result[storName]) {
                        console.log('存在， 更新 或者新增')

                        chrome.storage.local.set({ [storName]: result[storName].filter(item => item.id !== event.data.id) }, function () {
                            console.log('删除成功');
                            outActivityList()
                            sendAllPageMessage()
                            window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '删除成功' }, '*')

                        })
                    } else {
                        console.log('没有可以删除的内容')
                    }
                })
                break;
            case 'clear':
                chrome.storage.local.set({ [storName]: [] }, function () {
                    console.log('清空成功');
                    outActivityList()
                    sendAllPageMessage()
                    window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '清空成功' }, '*')
                })
            case 'delete_prodect':
                chrome.storage.local.get([storName], function (result) {
                    // console.log('result.initTiyangfenData', result.initTiyangfenData);
                    if (result[storName]) {
                        console.log('idid', event.data.id, event.data.product_id)


                        chrome.storage.local.set({
                            [storName]: result[storName].map(item => {
                                if (item.id === event.data.id) {
                                    item.addLtaLpConfig.promotion_goods = item.addLtaLpConfig.promotion_goods.filter(item => item.product_id !== event.data.product_id)
                                }
                                return item
                            })
                        }, function () {
                            outActivityList()
                            sendAllPageMessage()
                            window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '删除成功' }, '*')
                        })
                        // chrome.storage.local.set({ [storName]: result[storName].filter(item => item.id !== event.data.id) }, function () {
                        //     console.log('删除成功');
                        //     outActivityList()
                        //     window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'message', data: '删除成功' }, '*')
                        // })
                    }
                })
            case 'doudian_user_loginstatus':
                chrome.extension.sendMessage({ type_urs: 'wtbg', type_page: 'pageMeat', method: 'doudian_user_loginstatus', shop_id: event.data.shop_id }, null, function (e) {
                    // resolve(e)
                    console.log('向页面发送0621', e)
                })
            default:
                break;
        }
    }
    if (event.data.type_urs === 'wtbg') {
        switch (event.data.method) {
            case 'get_add_new_prd':
                window.postMessage({ type_urs: 'wtbg', method: 'getNewPrdHoter', data: '2.0.0' }, '*')
                break;
            default:
                break;
        }

    }
}
export function outActivityList() {
    chrome.storage.local.get([storName], function (result) {
        // console.log('result.initTiyangfenData', result.initTiyangfenData);
        window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'get', data: result[storName] || [] }, '*')
    });
}
export function outAutoActivityLogs() {
    chrome.storage.local.get([storLogName], function (result) {
        // console.log('result.initTiyangfenData', result.initTiyangfenData);
        window.postMessage({ type: 'dly_activity_modole_out', dly_method: 'get_logs', data: result[storLogName] || [] }, '*')
    });
}

function sendAllPageMessage() {
    chrome.extension.sendMessage({ type_urs: 'wtbg', type_page: 'pageMeat', method: 'dly_reload_all_page_autolist' }, null, function (e) {
        // resolve(e)
        console.log('发送成功 全部页面', e)
    })
}


function getPageMessage(botToken, chatId, text) {
    console.log('启动监听')
    window.removeEventListener('message', listenerFunction)
    window.addEventListener('message', listenerFunction)
}
export default getPageMessage();